(define (flatmap proc seq)
    (define (accumulate op initial sequence)
        (if (null? sequence)
            initial
            (op 
                (car sequence)
                (accumulate op initial (cdr sequence))
            )
        )
    )
    (accumulate append `() (map proc seq))
)

(define (enumerate-interval low height)
    (if (> low height)
        `()
        (cons low (enumerate-interval (+ low 1) height))
    )
)

(define (unique-pairs n)   
        (flatmap
            (lambda (i) 
                    (flatmap
                        (lambda (j)                            
                            (map 
                                (lambda (k)
                                    (list i j k) 
                                )
                                (enumerate-interval 1 (- j 1))       
                            )
                        )
                        (enumerate-interval 1 (- i 1)) 
                    )
            )
            (enumerate-interval 1 n)
            
        )       
)
(display (unique-pairs 4))
(newline)


(define (sum-pairs n)
    (define (check-sum? pair)
        (= n 
            (+
                (car pair)
                (cadr pair)
                (caddr pair) 
            )
        )
    )    
    (filter 
        check-sum?
        (unique-pairs n)
    ) 
    
)
(display (sum-pairs 10))
(exit)
